<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>菜单管理</title>
    <link rel="stylesheet" href="/static/css/layui.css" />
    <link rel="stylesheet" href="/static/css/common.css" />
</head>

<body class="container">

    <div class="layui-card">
        <div class="layui-card-body">
            <table id="power-table" lay-filter="power-table"></table>
        </div>
    </div>

    <script type="text/html" id="power-toolbar">
            <button class="layui-btn layui-btn-primary layui-btn-md" lay-event="add">
                <i class="layui-icon layui-icon-add-1"></i>
        新增
    </button>
            <button class="layui-btn layui-btn-success layui-btn-md" lay-event="expandAll">
                <i class="layui-icon layui-icon-spread-left"></i>
	    展开
	</button>
            <button class="layui-btn layui-btn-success layui-btn-md" lay-event="foldAll">
                <i class="layui-icon layui-icon-shrink-right"></i>
	    折叠
	</button>
        </script>

    <script type="text/html" id="power-bar">
            <button class="layui-btn layui-btn-primary layui-btn-sm" lay-event="edit">
                <i class="layui-icon layui-icon-edit"></i>
            </button>
            <button class="layui-btn layui-btn-danger layui-btn-sm" lay-event="remove">
                <i class="layui-icon layui-icon-delete"></i>
            </button>
        </script>

    <script type="text/html" id="power-type">
            {{#if (d.type == '0') { }} <span> 目录</span> {{# }else if(d.type == '1'){ }} <span> 菜单</span> {{# }else if(d.type == '2'){ }} <span> 按钮</span> {{# }else if(d.type == '3'){ }} <span> 权限</span> {{# } }}
        </script>

    <script type="text/html" id="power-enable">
            <input type="checkbox" name="ifshow" value="{{d.id}}" lay-skin="switch" lay-text="显示|不显示" lay-filter="ifshow"  {{ d.ifshow < 1 ? 'checked' : '' }}/>
        </script>

    <script type="text/html" id="icon">
            <i class="layui-icon {{d.icon}}"></i> {{d.title}}
        </script>

    <script src="/static/js/layui.js"></script>
    <script>
        layui.extend({
            api: '{/}/static/js/api' // 开头特定符 {/} 即代表采用单独路径
        }).use([
            'api'
        ], function () {
            let table = layui.table;
            let form = layui.form;
            let $ = layui.jquery;
            let treetable = layui.treeTable;
            let api = layui.api;

            let MODULE_PATH = "./";
            let cols = [
                [
                    {
                        field: 'id',
                        title: 'ID',
                        width:80
                    }, {
                        field: 'name',
                        minWidth: 300,
                        title: '权限名称',
                        templet: '#icon'
                    }, {
                        field: 'order_num',
                        edit: 'text',
                        title: '排序'
                    }, {
                        field: 'type',
                        title: '权限类型',
                        templet: '#power-type'
                    }, {
                        field: 'ifshow',
                        title: '是否显示',
                        templet: '#power-enable'
                    }, {
                        title: '操作',
                        templet: '#power-bar',
                        width: 210,
                        align: 'center'
                    }
                ]
            ];
            treetable.render({
                toolbar: '#power-toolbar',
                elem: '#power-table',
                url: api.apiUrl + 'menu/list',
                headers : api.getHeader(),
                page: false,
                cols,
                tree : {
                    view : {
                        showIcon : false,
                        icon : ""
                    }
                    //customName : {
                    //    name : 'title'
                    //}
                }
            });

            table.on('tool(power-table)', function (obj) {
                if (obj.event === 'remove') {
                    window.remove(obj);
                } else if (obj.event === 'edit') {
                    window.edit(obj);
                }
            })

            table.on('toolbar(power-table)', function (obj) {
                if (obj.event === 'add') {
                    window.add();
                } else if (obj.event === 'refresh') {
                    window.refresh();
                } else if (obj.event === 'expandAll') {
                    treetable.expandAll("power-table", true);
                } else if (obj.event === 'foldAll') {
                    treetable.expandAll("power-table", false);
                }
            });
            table.on('edit(power-table)', function (obj) {
                //console.log(obj)

                let update = {
                    id: obj.data.id,
                    field: obj.field,
                    value: obj.value
                }
                //console.log(update)
                //return;
                api.post('menu/editData', update, res => {
                    //console.log(res)
                    layer.msg('更新成功', {
                        icon: 1,
                        time: 1000
                    });
                }, err => {
                    //console.log(err)

                })
            });
            form.on('switch(ifshow)', function (data) {
                let v = 1;
                if (data.elem.checked)
                    v = 0;
                let postData = {
                    id: data.value,
                    ifshow: v
                };
                api.post('menu/ifshow', postData, res => {
                    //console.log(res)
                }, err => {
                    $(data.elem).prop('checked', false);
                    form.render('checkbox');
                })
            });
            window.add = function () {
                layer.open({
                    type: 2,
                    title: '新增',
                    shade: 0.1,
                    offset: 'rt',
                    area: [
                        '80%', '100%'
                    ],
                    anim: 1,
                    maxmin: true,
                    content: MODULE_PATH + 'add.html'
                });
            }
            

            window.edit = function (obj) {
                //console.log(obj)
                layer.open({
                    type: 2,
                    title: '修改',
                    shade: 0.1,
                    offset: 'rt',
                    area: [
                        '80%', '100%'
                    ],
                    anim: 1,
                    maxmin: true,
                    content: MODULE_PATH + 'edit.html?id=' + obj.data.id
                });
            }
            window.remove = function (obj) {
                layer.confirm('确定要删除该权限', {
                    icon: 3,
                    title: '提示'
                }, (index) => {
                    layer.close(index);
                    api.post('menu/del', {
                        id: obj.data.id
                    }, res => {
                        layer.msg('删除成功', {
                            icon: 1,
                            time: 1000
                        }, () => {
                            obj.del();
                        });
                    }, err => {
                        //layer.msg('删除失败',{icon:2,time:1000});
                    })
                });
            }

        })
    </script>
</body>

</html>